package com.sk.ccflower.lib.util.reflect;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author  Eunwook Lee
 */
public class AnnotationPropertyFilter implements PropertyFilter {

	public static final int METHOD_BOTH = 0;
	
	public static final int METHOD_GETTER = 1;
	
	public static final int METHOD_SETTER = 2;

	private Class<? extends Annotation> annotationClass;
	private int methodType = METHOD_BOTH;

	public AnnotationPropertyFilter(Class<? extends Annotation> annotationClass) {
		this.annotationClass = annotationClass;
	}

	public AnnotationPropertyFilter(Class<? extends Annotation> annotationClass, int methodType) {
		this.annotationClass = annotationClass;
		this.methodType = methodType;
	}
	
	/*
	 * implements kr.ideapool.web.lang.PropertyFilter
	 */

	public boolean accept(PropertyDescriptor pd) {
		Method getter = pd.getReadMethod();
		Method setter = pd.getWriteMethod();

		switch (methodType) {
		case METHOD_GETTER:
			return (getter != null && getter.isAnnotationPresent(annotationClass));

		case METHOD_SETTER:
			return (setter != null && setter.isAnnotationPresent(annotationClass));

		case METHOD_BOTH:
		default:
			return (getter != null && getter.isAnnotationPresent(annotationClass))
					|| (setter != null && setter.isAnnotationPresent(annotationClass));
		}
	}
	
	/*
	 * getters
	 */

	/**
	 * @return
	 */
	public Class<? extends Annotation> getAnnotationClass() {
		return annotationClass;
	}

	/**
	 * @return
	 */
	public int getMethodType() {
		return methodType;
	}

}
